<template>
	<view v-show="pageShow">
		<form @submit="formSubmit">
			<view class="uni-form-item">
				<view class="title">盘点单号</view>
				<input class="uni-input border disabled" disabled v-model="checkNo" />
			</view>
			<view class="uni-form-item">
				<view class="title">盘点货位</view>
				<view class="uni-input-container">
					<view class="uni-input-view">
						<input class="uni-input" v-model="goodsLocationCode" :focus="true" @input="handleInput" @confirm="handleConfirmGoodsLocationCode"
						 placeholder="请扫描盘点货位" />
						<image src="/static/scan.png" class="scan" @click="handleScanGoodsLocationCode" />
					</view>
					<image src="/static/search.png" class="search" @click="handleConfirmGoodsLocationCode" />
				</view>
			</view>
			<view class="uni-form-item">
				<view class="title">货品代码</view>
				<view class="uni-input-container">
					<view class="uni-input-view">
						<input class="uni-input" v-model="goodsCode" @input="handleInput" @confirm="handleConfirmGoodsCode" placeholder="请扫描货品代码" />
						<image src="/static/scan.png" class="scan" @click="handleScanGoodsCode" />
					</view>
					<image src="/static/search.png" class="search" @click="handleConfirmGoodsCode" />
				</view>
			</view>
			<view class="uni-form-item">
				<view class="title">货品名称</view>
				<input class="uni-input border disabled" disabled v-model="goodsName" />
			</view>
			<view class="uni-form-item" v-if="detail.isBlindDisk === '0'">
				<view class="title">库存数量</view>
				<input class="uni-input border disabled" disabled v-model="stockNumber" />
			</view>
			<view class="uni-form-item" v-if="detail.isBlindDisk === '0'">
				<view class="title">初盘数量</view>
				<input class="uni-input border disabled" disabled v-model="checkNumber" />
			</view>
			<view class="uni-form-item">
				<view class="title">盘点数量</view>
				<input class="uni-input border" type="number" v-model="replayCheckNumber" placeholder="请输入盘点数量" />
			</view>
			<view class="uni-btn-v" style="margin-top: 60rpx;" v-if="!!list.length">
				<button type="primary" form-type="submit">盘点</button>
			</view>
		</form>
		<uni-popup ref="goodsSelect" type="bottom">
			<view class="table">
				<view class="table-head">
					<view class="th">
						<view class="td" style="flex: 1;"></view>
						<view class="td" style="flex: 3;">货品代码</view>
						<view class="td" style="flex: 3;">批次号</view>
						<view class="td" style="flex: 2;" v-if="detail.isBlindDisk === '0'">库存数量</view>
					</view>
				</view>
				<scroll-view style="max-height: 600rpx;" scroll-y="true">
					<view class="table-body">
						<view class="tr" v-for="item in waitReplayGoodsList" :key="item.id" @click="handleSelect(item)">
							<view class="td" style="flex: 1;">
								<radio :checked="checkedReplayId === item.id" />
							</view>
							<view class="td" style="flex: 3;">{{item.goodsCode || '-'}}</view>
							<view class="td" style="flex: 3;">{{item.receiveCargoNo || '-'}}</view>
							<view class="td" style="flex: 2;" v-if="detail.isBlindDisk === '0'">{{parseInt(item.stockNumber)}}</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageShow: true,
				checkId: null,
				checkNo: null,
				goodsLocationCode: null,
				goodsCode: null,
				goodsName: null,
				stockNumber: null,
				checkNumber: null,
				replayCheckNumber: null,
				list: [],
				checkedReplayId: null,
				waitReplayGoodsList: [],
				detail: {
					ownerId: null,
					isBlindDisk: '0',
					isDefault: '0'
				}
			}
		},
		onLoad(e) {
			this.checkId = e.id;
			this.checkNo = e.checkNo;
			uni.showLoading({
				title: '加载中'
			});
			this.fetch();
		},
		onNavigationBarButtonTap() {
			uni.navigateTo({
				url: `/pages/stockCheck/replayDetail?id=${this.checkId}`
			});
		},
		methods: {
			handleInput() {
				this.goodsName = null;
				this.stockNumber = null;
				this.checkNumber = null;
				this.replayCheckNumber = null;
				this.checkedReplayId = null;
			},
			handleScanGoodsLocationCode() {
				this.$scan(res => {
					this.goodsLocationCode = res;
					this.handleConfirmGoodsLocationCode();
				});
			},
			handleConfirmGoodsLocationCode() {
				this.getWaitReplayGoods();
			},
			handleScanGoodsCode() {
				this.$scan(res => {
					this.goodsCode = res;
					this.handleConfirmGoodsCode();
				});
			},
			handleConfirmGoodsCode() {
				this.getWaitReplayGoods();
			},
			getWaitReplayGoods() {
				this.waitReplayGoodsList = [...this.list];
				if (this.goodsLocationCode) {
					this.waitReplayGoodsList = this.waitReplayGoodsList.filter(v => v.goodsLocationCode.toLowerCase().indexOf(this.goodsLocationCode
						.toLowerCase()) > -1);
				}
				if (this.goodsCode) {
					this.$api.goods.listScan({
						ownerId: this.detail.ownerId,
						code: this.goodsCode
					}).then(res => {
						const _goodsCodeList = res.data.map(v => v.code.toLowerCase());
						this.waitReplayGoodsList = this.waitReplayGoodsList.filter(v => _goodsCodeList.includes(v.goodsCode.toLowerCase()));
						this.getWaitReplayGoodsCallback();
					});
				} else {
					this.getWaitReplayGoodsCallback();
				}
			},
			getWaitReplayGoodsCallback() {
				if (!this.waitReplayGoodsList.length) {
					uni.showToast({
						title: '未匹配到待盘点货品',
						icon: 'none'
					});
					return;
				} else if (this.waitReplayGoodsList.length > 1) {
					this.$refs.goodsSelect.open();
				} else {
					const _goods = this.waitReplayGoodsList[0];
					this.handleSelect(_goods);
				}
			},
			handleSelect(goods) {
				this.checkedReplayId = goods.id;
				this.goodsLocationCode = goods.goodsLocationCode;
				this.goodsCode = goods.goodsCode;
				this.goodsName = goods.goodsName;
				this.stockNumber = parseInt(goods.stockNumber);
				this.checkNumber = parseInt(goods.checkNumber);
				if (this.detail.isDefault === '1') {
					this.replayCheckNumber = this.checkNumber;
				}
				setTimeout(() => {
					this.$refs.goodsSelect.close();
				}, 100);
			},
			fetch() {
				Promise.all([this.$api.stockCheck.detail(this.checkId), this.$api.stockCheck.replayPlateList({
					checkId: this.checkId,
					page: 1,
					limit: 1000
				})]).then(res => {
					uni.hideLoading();
					this.pageShow = true;
					this.detail = {
						ownerId: res[0].ownerId,
						isBlindDisk: res[0].isBlindDisk,
						isDefault: res[0].isDefault
					};
					this.list = res[1].data.filter(v => v.initialState === '300');
					if (!this.list.length) {
						uni.showToast({
							title: '已无待复盘货品',
							icon: 'none'
						});
					} else if (this.list.length === 1) {
						const _goods = this.list[0];
						this.handleSelect(_goods);
					}
				});
			},
			formSubmit(e) {
				if (!this.checkedReplayId) {
					uni.showToast({
						title: '请扫描待盘点货品',
						icon: 'none'
					});
					return;
				}
				if (!this.replayCheckNumber) {
					uni.showToast({
						title: '请输入盘点数量',
						icon: 'none'
					});
					return;
				}
				if (this.replayCheckNumber < 0) {
					uni.showToast({
						title: '盘点数量不能小于0',
						icon: 'none'
					});
					return;
				}
				uni.showLoading({
					title: '盘点中'
				});
				const params = {
					id: this.checkedReplayId,
					checkNumber: this.replayCheckNumber,
					trimNumber: this.replayCheckNumber - this.stockNumber
				};
				this.$api.stockCheck.updateReplayStock(params).then(res => {
					this.list = this.list.filter(v => v.id !== this.checkedReplayId);
					global.$refresh = true;
					uni.hideLoading();
					this.handleClear();
					uni.showToast({
						title: '盘点成功',
						icon: 'success',
						complete: () => {
							if (!this.list.length) {
								setTimeout(() => {
									uni.showToast({
										title: '已无待复盘货品',
										icon: 'none'
									});
									setTimeout(() => {
										uni.navigateBack();
									}, 500);
								}, 1000);
							} else if (this.list.length === 1) {
								const _goods = this.list[0];
								this.handleSelect(_goods);
							}
						}
					});
				}, () => uni.hideLoading());
			},
			handleClear() {
				this.goodsLocationCode = null;
				this.goodsCode = null;
				this.goodsName = null;
				this.stockNumber = null;
				this.checkNumber = null;
				this.replayCheckNumber = null;
				this.checkedReplayId = null;
				this.waitReplayGoodsList = [];
			}
		}
	}
</script>
